// 通用方法
export default {
  regexp: {
    phone: /^1[3456789]{1}\d{9}$/,
    email:
      /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/,
    integer: /^(0|[1-9][0-9]*)$/,
    floor: /^\d+(\.\d{1,4})?$/,
    number: /^\d*$/,
    bankCard: /^([1-9]{1})(\d{14}|\d{18})$/,
    qq: /^[1-9][0-9]{4,9}$/gim,
    scc: /^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/,
    chinese: /[\u4e00-\u9fa5]/, // 是否中文
    code: /^[1-9]\d{5}$/g, // 邮政编码
    IdCard: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, // 身份证
    pwd: /(?=.*[0-9])(?=.*[a-zA-Z]).{6,32}/, // 密码必须包含字母、数字，至少6个字符，最多32个字符
    ip: /(\d+)\.(\d+)\.(\d+)\.(\d+)/, // ip地址
  },
  // 检查微信环境
  checkIfWechat() {
    const ua = window.navigator.userAgent;
    return /MicroMessenger/.test(ua);
  },
  // 检查支付宝环境
  checkAlipay() {
    const ua = window.navigator.userAgent;
    return /AlipayDefined/.test(ua);
  },
  // 检查 ios 环境
  checkIfIos() {
    const ua = window.navigator.userAgent;
    return /\(i[^;]+;( U;)? CPU.+Mac OS X/.test(ua);
  },
  // 检查 android 环境
  checkIfAndroid() {
    const ua = window.navigator.userAgent;
    return /Android/i.test(ua);
  },
  /**
   * date * 时间对象
   * options * 配置参数
   * dayDelimiter String, * 年月日分隔符，默认 -
   * timeDelimiter String, * 时分秒分隔符，默认 :
   * ifHaveTime Boolean, * 是否需要时分秒，默认 false
   */
  formatDate(date, options) {
    if (!date || !date.getFullYear()) {
      throw new Error("请传入时间对象");
    }
    options = options || {};
    options.dayDelimiter = options.dayDelimiter || "-";
    options.timeDelimiter = options.timeDelimiter || ":";
    let str = "";
    str =
      date.getFullYear() +
      options.dayDelimiter +
      (date.getMonth() + 1) +
      options.dayDelimiter +
      date.getDate();
    if (options.ifHaveTime) {
      str +=
        " " +
        date.getHours() +
        options.timeDelimiter +
        date.getMinutes() +
        options.timeDelimiter +
        date.getMilliseconds();
    }
    return str;
  },
  /**
   * 动态加载 js 文件
   * opts: {} 配置参数
   * obj String, * 加载 js 文件后的 全局对象 必填
   * url String, * 加载 js 的路径，必填
   * callback Function, 加载完毕后的回调函数，如果对象存在，直接触发回调
   */
  loadJS(opts) {
    if (!(opts.obj && opts.obj !== 0)) {
      throw new Error("请传入全局对象名");
    }
    if (!opts.url) {
      throw new Error("请传入js资源地址");
    }
    if (window[opts.obj]) {
      return opts.callback && opts.callback();
    }
    const oScript = document.createElement("script");
    oScript.src = opts.url;
    oScript.onload = () => {
      opts.callback && opts.callback();
    };
    document.body.appendChild(oScript);
  },
  //js 加法计算
  //调用：accAdd(arg1,arg2)
  //返回值：arg1加arg2的精确结果
  accAdd(arg1, arg2) {
    var r1, r2, m;
    try {
      r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    return ((arg1 * m + arg2 * m) / m).toFixed(2);
  },
  //js 减法计算
  //调用：Subtr(arg1,arg2)
  //返回值：arg1减arg2的精确结果
  Subtr(arg1, arg2) {
    var r1, r2, m , n;
    try {
      r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    //last modify by deeka
    //动态控制精度长度
    n = r1 >= r2 ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(2);
  },
  //js 乘法函数
  //调用：accMul(arg1,arg2)
  //返回值：arg1乘以arg2的精确结果
  accMul(arg1, arg2) {
    var m = 0,
      s1 = arg1.toString(),
      s2 = arg2.toString();
    try {
      m += s1.split(".")[1].length;
    } catch (e) {}
    try {
      m += s2.split(".")[1].length;
    } catch (e) {}
    return (
      (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) /
      Math.pow(10, m)
    );
  },
  getQueryString(name) {
    const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    const text = window.location.search.substr(1).match(reg);
    if (text) {
      return unescape(text[2]);
    }
    return "";
  },
};
